package com.itheima.tlias.controller;

import com.github.pagehelper.Page;
import com.itheima.tlias.annotation.LogOperate;
import com.itheima.tlias.pojo.PageResult;
import com.itheima.tlias.pojo.Result;
import com.itheima.tlias.pojo.Student;
import com.itheima.tlias.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private final StudentService studentService;
    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }
    //分条件查询
    @GetMapping
    public Result queryStudent(String name,
                               Integer degree,
                               Integer clazzId,
                               @RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int pageSize) {
        Student student = Student.builder().name(name).degree(degree).clazzId(clazzId).build();
        List<Student> students = studentService.queryStudent(student, page, pageSize);
        Long total =((Page)students).getTotal();
        return Result.success(new PageResult(total, students));
    }
    //删除学员
    @DeleteMapping("/{ids}")
    @LogOperate
    public Result delete(@PathVariable List<Integer> ids) {
        Integer row = studentService.delete(ids);
        if (row == 0) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
    //添加学员
    @PostMapping
    @LogOperate
    public Result insert(@RequestBody Student student) {
        Integer row = studentService.insert(student);
        if (row == 0) {
            return Result.error("添加失败");
        }
        return Result.success();
    }
    //修改学员
    @PutMapping
    @LogOperate
    public Result update(@RequestBody Student student) {
        Integer row = studentService.update(student);
        if (row == 0) {
            return Result.error("修改失败");
        }
        return Result.success();
    }
    //修改时的回显操作
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Student student = studentService.getById(id);
        return Result.success(student);
    }
    //违纪处理操作
    @PutMapping("/violation/{id}/{score}")
    @LogOperate
    public Result violation(@PathVariable Integer id, @PathVariable Short score) {
        if (score < 0) {
            return Result.error("扣分不能小于0");
        }
        Short count = studentService.getById(id).getViolationCount() ;
        int newCount = count + 1;
        // 检查 newCount 是否在 short 类型的范围内
        if (newCount < Short.MIN_VALUE || newCount > Short.MAX_VALUE) {
            return Result.error("违纪次数超出范围");
        }
        count = (short) newCount;
        Student student = Student.builder().id(id).violationCount(count).violationScore(score).build();
        Integer row = studentService.update(student);
        if (row == 0) {
            return Result.error("修改失败");
        }
        return Result.success();
    }

}
